package org.algorithm.二师兄.数组.二月;

import java.util.Arrays;

public class 三数之和 {

    public static void main(String[] args) {
        int[] arr = {3,8,5,0,9,2,1};
        int target = 10;
        Arrays.sort(arr);
        System.out.println("数组：： " + Arrays.toString(arr));
        int[] twoSum = threeSum(arr, target);
        System.out.println(Arrays.toString(twoSum));
    }

    private static int[] threeSum(int[] arr, int target) {
        if (arr == null || arr.length == 0) return arr;
        int right = arr.length - 1;
        for (int i = 0; i < arr.length; i++) {
            int left = i + 1;
            while (left != right) {
                if (arr[left] + arr[right] + arr[i] < target) {
                    left++;
                }else if (arr[left] + arr[right] + arr[i] > target) {
                    right--;
                }else if (arr[left] + arr[right] + arr[i] == target) {
                    return new int[]{i, left, right};
                }
            }
        }
        return arr;
    }
}
